package com.ark.frigate.dingtalk.ding.message;

import org.apache.commons.lang3.ArrayUtils;

import com.ark.frigate.dingtalk.ding.MsgType;
import com.ark.frigate.dingtalk.utils.DingTalkMessageJson;

import lombok.Data;

@Data
public class MarkDownMessage extends AbstractMessage {

    private MarkDown markdown;

    private String[] atMobiles;

    @Override
    public String toJson() {
        MarkDownFormat markDownFormat = new MarkDownFormat();
        markDownFormat.setMsgtype(MsgType.markdown);

        Mobiles mobiles = new Mobiles();
        mobiles.setAtMobiles(atMobiles);
        mobiles.setIsAtAll(false);
        if (ArrayUtils.isEmpty(atMobiles)) {
            mobiles.setIsAtAll(true);
        }

        markDownFormat.setAt(mobiles);
        markDownFormat.setMarkdown(markdown);
        return DingTalkMessageJson.toJson(markDownFormat);
    }

    public Boolean setMarkDown(String title, String text) {
        if (markdown == null) {
            markdown = new MarkDown();
        }
        markdown.setTitle(title);
        markdown.setText(text);
        return true;
    }

    @Data
    class MarkDownFormat {

        private MsgType msgtype;

        private MarkDown markdown;

        private Mobiles at;
    }

    @Data
    class MarkDown {

        private String title;

        private String text;
    }
}
